package cloud.ben.smallscalepigfarmmanagementsystem.service.impl.pig;

import cloud.ben.smallscalepigfarmmanagementsystem.entity.pig.PigSalesRecords;
import cloud.ben.smallscalepigfarmmanagementsystem.entity.pig.PigTransferFattening;
import cloud.ben.smallscalepigfarmmanagementsystem.mapper.pig.PigMapper;
import cloud.ben.smallscalepigfarmmanagementsystem.mapper.pig.PigTransferFatteningMapper;
import cloud.ben.smallscalepigfarmmanagementsystem.service.pig.IPigTransferFatteningService;
import cloud.ben.smallscalepigfarmmanagementsystem.utils.RespBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Ben
 * @Date: 2022/4/6 11:34
 */
@Service
public class PigTransferFatteningServiceImpl implements IPigTransferFatteningService {

    protected static final Logger logger = LoggerFactory.getLogger(PigTransferFatteningServiceImpl.class);

    @Autowired
    private PigTransferFatteningMapper pigTransferFatteningMapper;


    @Override
    public RespBean insertPigTransferFattening(PigTransferFattening pigTransferFattening) {
        if (pigTransferFatteningMapper.insert(pigTransferFattening) == 1){
            logger.info("插入成功");
            return RespBean.ok("插入成功");
        }
        logger.error("系统异常，插入失败");
        return  RespBean.error("系统异常，插入失败");
    }

    @Override
    public RespBean deletePigTransferFattening(Integer id) {
        QueryWrapper<PigTransferFattening> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.eq("id", id);
        List<PigTransferFattening> list = pigTransferFatteningMapper.selectList(queryWrapper);
        if (list != null) {
            if (pigTransferFatteningMapper.delete(queryWrapper)!= 0){
                logger.info("删除成功");
                return  RespBean.ok("删除成功");
            }
            logger.error("系统异常，删除失败");
            return RespBean.error("系统异常，删除失败");
        }
        logger.info("您要删除的数据不存在，删除失败");
        return RespBean.error("您要删除的数据不存在，删除失败");
    }

    @Override
    public RespBean updatePigTransferFattening(PigTransferFattening pigTransferFattening) {
        QueryWrapper<PigTransferFattening> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.eq("id", pigTransferFattening.getId());
        List<PigTransferFattening> list = pigTransferFatteningMapper.selectList(queryWrapper);
        if (list != null) {
            if (pigTransferFatteningMapper.update(pigTransferFattening, queryWrapper)!= 0){
                logger.info("更新成功");
                return RespBean.ok("更新成功");
            }
            logger.error("系统异常，更新失败");
        }
        logger.error("您要更新的数据不存在，更新失败");
        return RespBean.error("您要更新的数据不存在，更新失败");
    }

    @Override
    public RespBean selectPigTransferFatteningByPage(Integer pageNumber, Integer pageSize) {
        Page<PigTransferFattening> page = new Page<>(pageNumber, pageSize);
        IPage<PigTransferFattening> pigTransferFatteningIPage =pigTransferFatteningMapper.selectPage(page, null);
        return RespBean.ok("查询成功",pigTransferFatteningIPage);
    }


    @Override
    public RespBean selectPigTransferFatteningNum() {
        return RespBean.ok("查询成功",  pigTransferFatteningMapper.selectCount(null));
    }
}